#################################################################################
# Replication File for 
# Pluemper & Traunmueller "The Sensitivity of Sensitivity Analysis" 
# Conditionally Accepted for Publication in PSRM
# Contact: traunmueller@soz.uni-frankfurt.de 
# Date: 14.8.17
# Last Changed: 12.12.17 (added set.seed() commands)
################################################################################

# Specify Working Directory
YOURPATH <- "/Users/richardtraunmuller/Dropbox/" # Change Your Path Here
setwd(paste(YOURPATH, "PSRM_Replication/", sep="")) 

# Load Required R Packages 
library(mvtnorm)
library(reshape2)  
library(plyr)

# Load Custom Functions
source("huerlimann_function.R") # Function to simulate NxN correlation matrizes
source("extract_fun_OLS.R") # Function to extract model quantities 

#################################################################################
# CAUTION: Full replication using this file will take veeeeery long! 
#
# Each simulation file can also (and probably should) be run separatly 
# in parallel. 
#
# It is also possible to just summarize the simulations
# saved in the folder.
#
#################################################################################
# Replication of Monte Carlo Study 1
#################################################################################

# Replicate Simulations (Takes Some Time)
set.seed(12345) # Set Random Seed for Exact Replication
source("PSRM_one_mcs.R")  # Runs Part A of the Simulations (p=1)

set.seed(12345) # Set Random Seed for Exact Replication
source("PSRM_five_mcs.R") # Runs Part B of the Simulations (p=5)

set.seed(12345) # Set Random Seed for Exact Replication
source("PSRM_nine_mcs.R") # Runs Part C of the Simulations (p=9)

# Simulation Results are Saved in the Following Files:
# one_result_mat_1.rds
# one_result_mat_2.rds
# one_result_mat_3.rds
# one_result_mat_4.rds
# one_result_mat_5.rds
# 
# five_result_mat_1.rds
# five_result_mat_2.rds
# five_result_mat_3.rds
# five_result_mat_4.rds
# five_result_mat_5.rds
# 
# nine_result_mat_1.rds
# nine_result_mat_2.rds
# nine_result_mat_3.rds
# nine_result_mat_4.rds
# nine_result_mat_5.rds

# Summary of Simulations (May Be Run Without Re-Running the Simulations)
source("PSRM_summary_1.R") # Produces a .csv File of Table 1 in the Paper

#             .25   .50   .75  1.00  1.25
# EBA_Leamer 0.867 0.540 0.178 0.023 0.004
# EBA_Leamer 0.038 0.035 0.035 0.036 0.034
# EBA_sign   0.049 0.008 0.000 0.000 0.000
# EBA_sign   0.927 0.915 0.907 0.901 0.877
# EBA_90     0.037 0.004 0.000 0.000 0.000
# EBA_90     0.952 0.944 0.933 0.927 0.909
# MA_unig    0.845 0.516 0.153 0.022 0.004
# MA_unig    0.049 0.046 0.046 0.048 0.048
# MA_Sala    0.907 0.642 0.274 0.045 0.005
# MA_Sala    0.018 0.019 0.018 0.019 0.018
# MA_BIC     0.965 0.779 0.442 0.113 0.020
# MA_BIC     0.005 0.005 0.005 0.006 0.006

# EBA_Leamer 0.862 0.558 0.221 0.046 0.006
# EBA_Leamer 0.040 0.034 0.025 0.024 0.020
# EBA_sign   0.055 0.020 0.001 0.000 0.000
# EBA_sign   0.916 0.874 0.821 0.773 0.719
# EBA_90     0.036 0.012 0.000 0.000 0.000
# EBA_90     0.946 0.920 0.887 0.851 0.808
# MA_unig    0.833 0.498 0.170 0.025 0.003
# MA_unig    0.050 0.044 0.046 0.046 0.043
# MA_Sala    0.912 0.641 0.266 0.057 0.007
# MA_Sala    0.020 0.018 0.017 0.018 0.017
# MA_BIC     0.967 0.793 0.439 0.134 0.021
# MA_BIC     0.005 0.005 0.004 0.005 0.005

# EBA_Leamer 0.864 0.581 0.264 0.070 0.010
# EBA_Leamer 0.030 0.022 0.015 0.015 0.014
# EBA_sign   0.064 0.023 0.003 0.000 0.000
# EBA_sign   0.893 0.827 0.731 0.632 0.587
# EBA_90     0.040 0.015 0.001 0.000 0.000
# EBA_90     0.931 0.897 0.842 0.797 0.757
# MA_unig    0.834 0.500 0.178 0.030 0.003
# MA_unig    0.040 0.038 0.038 0.039 0.042
# MA_Sala    0.909 0.643 0.280 0.063 0.007
# MA_Sala    0.011 0.015 0.016 0.014 0.018
# MA_BIC     0.964 0.789 0.454 0.152 0.030
# MA_BIC     0.001 0.001 0.006 0.005 0.003

#################################################################################
# Monte Carlo Study 2
#################################################################################

# Replicate Simulations (Takes Some Time)
set.seed(12345) # Set Random Seed for Exact Replication
source("PSRM_one_runif_mcs.R") # Runs Part A of the Simulations (p=1)

set.seed(12345) # Set Random Seed for Exact Replication
source("PSRM_five_runif_mcs.R") # Runs Part B of the Simulations (p=5)

set.seed(12345) # Set Random Seed for Exact Replication
source("PSRM_nine_runif_mcs.R") # Runs Part C of the Simulations (p=9)

# Simulation Results are Saved in the Following Files:
# one_runif_result_mat_1.rds
# one_runif_result_mat_2.rds
# one_runif_result_mat_3.rds
# one_runif_result_mat_4.rds
# one_runif_result_mat_5.rds
# 
# five_runif_result_mat_1.rds
# five_runif_result_mat_2.rds
# five_runif_result_mat_3.rds
# five_runif_result_mat_4.rds
# five_runif_result_mat_5.rds
# 
# nine_runif_result_mat_1.rds
# nine_runif_result_mat_2.rds
# nine_runif_result_mat_3.rds
# nine_runif_result_mat_4.rds
# nine_runif_result_mat_5.rds

# Summary of Simulations (May Be Run Without Re-Running the Simulations)
source("PSRM_summary_2.R") # Produces a .csv File of Table 2 in the Paper

#             .25   .50   .75  1.00  1.25
# EBA_Leamer 0.998 0.990 0.986 0.957 0.930
# EBA_Leamer 0.000 0.000 0.000 0.000 0.000
# EBA_sign   0.939 0.861 0.790 0.708 0.661
# EBA_sign   0.018 0.018 0.014 0.012 0.009
# EBA_90     0.693 0.544 0.412 0.318 0.248
# EBA_90     0.170 0.163 0.147 0.134 0.119
# MA_unig    0.998 0.972 0.926 0.858 0.758
# MA_unig    0.001 0.000 0.000 0.001 0.001
# MA_Sala    0.998 0.975 0.944 0.874 0.790
# MA_Sala    0.001 0.000 0.000 0.001 0.001
# MA_BIC     1.000 0.998 0.993 0.966 0.934
# MA_BIC     0.000 0.000 0.000 0.000 0.000

# EBA_Leamer 0.999 0.998 0.997 0.992 0.990
# EBA_Leamer 0.000 0.000 0.000 0.000 0.000
# EBA_sign   0.950 0.940 0.924 0.915 0.917
# EBA_sign   0.004 0.004 0.003 0.002 0.001
# EBA_90     0.722 0.664 0.604 0.575 0.556
# EBA_90     0.098 0.085 0.075 0.062 0.055
# MA_unig    0.995 0.980 0.952 0.922 0.899
# MA_unig    0.000 0.000 0.001 0.000 0.001
# MA_Sala    0.996 0.980 0.934 0.870 0.806
# MA_Sala    0.000 0.001 0.001 0.001 0.003
# MA_BIC     1.000 0.998 0.993 0.985 0.973
# MA_BIC     0.000 0.000 0.000 0.000 0.000

# EBA_Leamer 1.000 0.999 0.998 0.998 0.996
# EBA_Leamer 0.000 0.000 0.000 0.000 0.000
# EBA_sign   0.973 0.971 0.970 0.968 0.966
# EBA_sign   0.001 0.001 0.000 0.001 0.001
# EBA_90     0.795 0.772 0.746 0.738 0.731
# EBA_90     0.053 0.047 0.042 0.047 0.039
# MA_unig    0.997 0.988 0.973 0.961 0.948
# MA_unig    0.000 0.000 0.000 0.001 0.002
# MA_Sala    0.997 0.983 0.953 0.901 0.852
# MA_Sala    0.000 0.001 0.002 0.003 0.003
# MA_BIC     1.000 0.999 0.997 0.993 0.989
# MA_BIC     0.000 0.000 0.000 0.000 0.000

#################################################################################
# Monte Carlo Study 3
#################################################################################

# Replicate Simulations (Takes Some Time)
set.seed(12345) # Set Random Seed for Exact Replication
source("PSRM_five_norm_1_mcs.R") # Runs Part A of the Simulations [r ~ N(0, .1)]

set.seed(12345) # Set Random Seed for Exact Replication
source("PSRM_five_norm_3_mcs.R") # Runs Part B of the Simulations [r ~ N(0, .3)]

set.seed(12345) # Set Random Seed for Exact Replication
source("PSRM_five_norm_5_mcs.R") # Runs Part C of the Simulations [r ~ N(0, .5)]

# Simulation Results are Saved in the Following Files:
# five_norm_1_result_mat_1.rds
# five_norm_1_result_mat_2.rds
# five_norm_1_result_mat_3.rds
# five_norm_1_result_mat_4.rds
# five_norm_1_result_mat_5.rds
#
# five_norm_3_result_mat_1.rds
# five_norm_3_result_mat_2.rds
# five_norm_3_result_mat_3.rds
# five_norm_3_result_mat_4.rds
# five_norm_3_result_mat_5.rds
#
# five_norm_5_result_mat_1.rds
# five_norm_5_result_mat_2.rds
# five_norm_5_result_mat_3.rds
# five_norm_5_result_mat_4.rds
# five_norm_5_result_mat_5.rds

# Summary of Simulations (May Be Run Without Re-Running the Simulations)
source("PSRM_summary_3.R") # Produces a .csv File of Table 3 in the Paper

#             .25   .50   .75  1.00  1.25
# EBA_Leamer 0.911 0.697 0.413 0.167 0.066
# EBA_Leamer 0.016 0.014 0.008 0.008 0.005
# EBA_sign   0.177 0.072 0.018 0.003 0.001
# EBA_sign   0.711 0.602 0.485 0.405 0.314
# EBA_90     0.113 0.041 0.009 0.001 0.000
# EBA_90     0.811 0.732 0.659 0.580 0.518
# MA_unig    0.842 0.544 0.219 0.053 0.013
# MA_unig    0.038 0.041 0.042 0.043 0.042
# MA_Sala    0.917 0.657 0.303 0.075 0.014
# MA_Sala    0.015 0.018 0.025 0.032 0.036
# MA_BIC     0.971 0.828 0.526 0.225 0.078
# MA_BIC     0.004 0.004 0.002 0.005 0.004

# EBA_Leamer 0.982 0.938 0.866 0.762 0.677
# EBA_Leamer 0.003 0.002 0.000 0.000 0.000
# EBA_sign   0.552 0.449 0.357 0.298 0.274
# EBA_sign   0.235 0.145 0.096 0.064 0.043
# EBA_90     0.349 0.230 0.139 0.094 0.079
# EBA_90     0.467 0.370 0.299 0.255 0.221
# MA_unig    0.928 0.761 0.559 0.395 0.293
# MA_unig    0.011 0.017 0.020 0.024 0.025
# MA_Sala    0.955 0.781 0.494 0.253 0.132
# MA_Sala    0.007 0.016 0.038 0.069 0.109
# MA_BIC     0.992 0.941 0.836 0.696 0.584
# MA_BIC     0.000 0.002 0.001 0.002 0.002

# EBA_Leamer 0.997 0.990 0.974 0.961 0.947
# EBA_Leamer 0.000 0.000 0.000 0.000 0.000
# EBA_sign   0.876 0.833 0.815 0.792 0.779
# EBA_sign   0.024 0.014 0.009 0.006 0.004
# EBA_90     0.612 0.527 0.459 0.418 0.397
# EBA_90     0.178 0.141 0.113 0.096 0.087
# MA_unig    0.982 0.936 0.867 0.798 0.751
# MA_unig    0.001 0.001 0.002 0.002 0.002
# MA_Sala    0.988 0.936 0.827 0.680 0.583
# MA_Sala    0.001 0.002 0.005 0.010 0.019
# MA_BIC     0.998 0.992 0.967 0.929 0.903
# MA_BIC     0.000 0.000 0.000 0.000 0.000

#################################################################################



